import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "../components/Login";
import Home from "../components/Home";
import Welcome from "../components/Welcome";
import Users from "../components/user/Users";
import Rights from "../components/right/Rights"
import Roles from "../components/right/Roles";
import Categories from '../components/goods/Categories'
import Goods from "../components/goods/Goods";
import Params from "../components/goods/Params";
import AddGoods from "../components/goods/AddGoods";
import Orders from "../components/orders/Orders";
import Reports from "../components/reports/Reports";

Vue.use(VueRouter)

const router = new VueRouter({
    routes: [
        {path: '/', redirect: '/login'},
        {path: '/login', component: Login},
        {path: '/home', component: Home,children:[
                {path:'',redirect:'Welcome'},
                {path:'welcome',component:Welcome},
                {path:'users',component:Users},
                {path:'rights',component:Rights},
                {path:'roles',component:Roles},
                {path:'categories',component:Categories},
                {path:'goods',component:Goods},
                {path:'params',component:Params},
                {path:'goods/addgoods',component:AddGoods},
                {path:'orders',component:Orders},
                {path:'reports',component:Reports}
            ]}
    ]
})

//挂载路由守卫
router.beforeEach((to, from, next) => {
    //只有login不需要权限
    if (to.path == '/login') {
        return next();
    }
    //判断是否有token
    const token = window.sessionStorage.getItem('token')
    if (!token) {
        return next('/login')
    } else {
        next()
    }
})

export default router
